﻿package Engine.RMI
{
    import Framework.Serialize.*;
    import Framework.Util.*;

    public class Outgoing extends Object
    {

        public function Outgoing()
        {
            return;
        }// end function

        public static function dispatch(param1:Context, param2:SRMIReturn, param3:SerializeStream) : Boolean
        {
            if (!param1._connection)
            {
                return false;
            }
            var _loc_4:* = new SerializeStream();
            ERMIMessageType.MessageTypeCallRet.__write(_loc_4);
            param2.__write(_loc_4);
            _loc_4.writeByteArray(param3);
            return param1._connection.send(_loc_4.byteArray) != -1;
        }// end function

        public static function invokeAsync(param1:Context, param2:SRMICall, param3:SerializeStream, param4:RMIObject) : void
        {
            var _loc_6:Exception = null;
            if (RMIDispatcher.getInstance().hasEventListener(RMIEvent.SHOW_INTERFACE_TYPE))
            {
                RMIDispatcher.getInstance().dispatchEvent(new RMIEvent(RMIEvent.SHOW_INTERFACE_TYPE, false, false, _loc_6, param4.toString()));
            }
            if (param1._session.communicator == null || param1._connection && param1._connection.isConnnetClose)
            {
                _loc_6 = new Exception("ExceptionCodeNotInstantiation", RMIException.ExceptionCodeNotInstantiation);
                param4.cdeException(_loc_6);
                return;
            }
            var _loc_5:* = param1._session.communicator.prepareCommandHandler;
            if (_loc_5 != null)
            {
                if (!_loc_5.prepareCommand(param2, param1, param4))
                {
                    return;
                }
            }
            if (param4 == null)
            {
                invokeAsyncNoBack(param1, param2, param3);
            }
            else
            {
                invokeAsyncBack(param1, param2, param3, param4);
            }
            return;
        }// end function

        private static function invokeAsyncBack(param1:Context, param2:SRMICall, param3:SerializeStream, param4:RMIObject) : void
        {
            var _loc_6:Exception = null;
            var _loc_5:* = new RMIObjectBind();
            param4.identity = param2.identity;
            _loc_5.messageId = param1._session.addBackObject(_loc_5, param1._timeOut);
            _loc_5.rmiObject = param4;
            _loc_5.outgoingStatus = RMIObjectBind.EOutGoingWaitToSend;
            _loc_5.session = param1._session;
            var _loc_7:* = new SerializeStream();
            ERMIMessageType.MessageTypeCall.__write(_loc_7);
            param2.callSynchType = ERMICallModel.CallModelAsync;
            param2.messageId = _loc_5.messageId;
            if (param2.messageId <= 0)
            {
                _loc_6 = new Exception("Outgoing::context._session.addBackObject exception", Exception.ExceptionCodeBase);
                param4.cdeException(_loc_6);
            }
            param2.__write(_loc_7);
            _loc_7.writeByteArray(param3);
            if (param1._connection == null)
            {
                param1._connection = param1._session.connection;
            }
            _loc_5.buffer = _loc_7;
            if (param1._connection == null)
            {
                if (param1._session.communicator && !param1._session.communicator.connecting)
                {
                    if (param1._session.communicator.connect())
                    {
                        param1._session.addMessage(_loc_5);
                        return;
                    }
                }
                else
                {
                    param1._session.addMessage(_loc_5);
                }
            }
            else if (param1._connection.send(_loc_7.byteArray) == -1)
            {
                param1._session.findRemoveBackObject(param2.messageId);
                _loc_6 = new RMIException("Outgoing::context._connection.send exception", RMIException.ExceptionCodeConnectionWrite);
                param4.cdeException(_loc_6);
            }
            return;
        }// end function

        public static function invokeAsyncNoBack(param1:Context, param2:SRMICall, param3:SerializeStream) : void
        {
            var _loc_4:* = new RMIObjectBind();
            _loc_4.messageId = 0;
            _loc_4.rmiObject = null;
            _loc_4.outgoingStatus = RMIObjectBind.EOutGoingWaitToSend;
            var _loc_5:* = new SerializeStream();
            if (param1._messageType && param1._messageType.equals(ERMIMessageType.MessageTypeMQ))
            {
                param1._messageType.__write(_loc_5);
            }
            else
            {
                ERMIMessageType.MessageTypeCall.__write(_loc_5);
                param2.messageId = 0;
                param2.callSynchType = ERMICallModel.CallModelAsync;
                param2.__write(_loc_5);
            }
            _loc_5.writeByteArray(param3);
            if (param1._connection == null)
            {
                param1._connection = param1._session.connection;
            }
            if (param1._connection == null)
            {
                if (!param1._session.communicator.connecting)
                {
                    if (param1._session.communicator.connect())
                    {
                        _loc_4.buffer = _loc_5;
                        param1._session.addMessage(_loc_4);
                        return;
                    }
                }
            }
            else
            {
                param1._connection.send(_loc_5.byteArray);
            }
            return;
        }// end function

    }
}
